
var requests = require('../../requests/request.js');
var utils = require('../../utils/util.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    session3rd:'',
    isZan: 0,          //是否点赞
    resultData: null,     //测试结果
    test_id: '',           //试题id
    score: ''
  },
  onLoad: function (option) {
    // console.log(option)
    var that = this;
    var id = option.test_id;
    that.setData({
      test_id: id,
      score: option.score
    })
    wx.getStorage({
      key: '3rd_session',
      success: function(res) {
        var session = res.data;
        that.setData({ session3rd: session });
        requests.requestTestResult(      //请求测试结果
          {
            session:session,
            test_id: id,
            score: option.score
          }, (data) => {
            // console.log(data)
            that.setData({
              resultData: data,
              isZan: data.is_like
            })
          }, () => {
            //请求失败
          }, () => {
            //请求完成
          }
        )
      },
    })
    
  },
  //点赞
  zan: function () {       //点赞
    const that = this;
    const testId = this.data.test_id;
    const session = this.data.session3rd;
    var reData = this.data.resultData;
    requests.requestDianZan(
      {
        test_id: testId,
        session: session
      }, (res) => {
        // console.log(res);
        if (res.is_like) {              //点赞
          reData.like_num++;
          that.setData({
            isZan: 1,
            resultData: reData
          });
        } else {                        //取消点赞
          reData.like_num--;
          that.setData({
            isZan: 0,
            resultData: reData
          });
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }
    )
  },
  //重新测试
  retest: function () {
    wx.navigateBack({
      delta: 1    //返回上一级页面
    })
  },
  //更多测试
  moreTest: function () {
    // wx.navigateBack({
    //   delta: 2    //返回上二级页面
    // })
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //分享
  // onShareAppMessage: function () {
  //   var test_id = this.data.test_id;
  //   var score = this.data.score;
  //   return {
  //     title: '郭半仙',
  //     path: '/pages/testresult/testresult?test_id=' + test_id + '&score=' + score,
  //     success: function (res) {
  //       // 分享成功
  //     },
  //     fail: function (res) {
  //       // 分享失败
  //     }
  //   }
  // }
})